/*
 * Created on Feb 12, 2008
 */
package maxygen;

import java.util.regex.*;

/**
 * For storing Max style object names e.g., name, name[1] etc.
 * 
 * @author martinrobinson
 */
public class ObjectName {
    private String name;
    private int index;
    private int subIndex;
    
    public ObjectName(String n, int i)
    {
        name = n;
        index = i;
        subIndex = 0;
    }
    
    public ObjectName(String n)
    {
        setName(n);
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getFullName()
    {
        if(subIndex == 0) {
            if(index == 0)
                return name;
            else
                return name+"["+index+"]";
        } else {
            return name+"["+index+"]["+subIndex+"]";
        }
    }
    
    public void setNameOnly(String n)
    {
        name = n;
    }
    
    public void setName(String n)
    {
        if(n == null) {
            name = "";
            index = 0;
            return;
        }
        
        Pattern p = Pattern.compile("(.+)\\[(\\d+)\\]");
        Matcher m = p.matcher(n);
        
        if(m.find()) {
            name = m.group(1);
            index = Integer.parseInt(m.group(2));
            
            m = p.matcher(name);
            
            if(m.find()) {
                name = m.group(1);
                subIndex = index;
                
                //subIndex = Integer.parseInt(m.group(2));
                index = Integer.parseInt(m.group(2));
                
            } else {
                subIndex = 0;
            }
            
        } else {
            name = n;
            index = 0;
        }
    }
    
    public int getIndex()
    {
        return index;
    }
    
    public void setIndex(int i)
    {
        index = i;
    }
    
    public int getSubIndex()
    {
        return subIndex;
    }
    
    public void setSubIndex(int i)
    {
        subIndex = i;
    }
}
